<?php

class VisitService extends AbstractService
{

    /**
     * @var UserService
     */
    private static $instance;

    /**
     * Get instance of VisitService.
     *
     * @return VisitService
     */
    public static function get()
    {
        if (!self::$instance) {
            self::$instance = new VisitService();
        }
        return self::$instance;
    }

    /**
     * Construction.
     */
    protected function __construct()
    {
        parent::__construct();
    }

    /**
     * 处理创建时的逻辑
     *
     * @param int $shardId
     * @return void
     */
    public function handleCreate($shardId)
    {

    }

    /**
     * 处理登录时的逻辑
     *
     * @param int $userId
     * @return void
     */
    public function handleLogin($userId)
    {

    }

    /**
     * 提供当前 service 数据
     *
     * @param int $userId
     * @param boolean $visitMode default false
     * @return array()
     */
    public function info($userId, $visitMode = false)
    {
        return [];
    }

    /**
     * 添加用户拜访历史
     *
     * @param int $userId
     * @param int $visitUserId
     * @return boolean  如果今天已经访问过则返回false，没有返回true
     */
    public function addVisitRecord($userId, $visitUserId)
    {
        $reqTime = GameService::get()->getReqTime();
        $key = GameConst::CACHE_KEY_VISIT_USER_RECORD . $visitUserId;
        if ($this->context->getAppCache($visitUserId)->hGet($key, $userId)) {
            return false;
        }
        $this->context->getAppCache($visitUserId)->hSet($key, $userId, 1, SzTime::getNextDayTimestamp($reqTime) - $reqTime);
        return true;
    }

    /**
     * 获取用户信息.
     *
     * @param int $userId
     * @throws SzException 20003
     * @return array
     */
    public function init($userId)
    {
        list($profile, $profileInfoList) = UserService::get()->info($userId);
        if (!$profile) {
            throw new SzException(20003, $userId);
        }

        list($groundList) = GroundService::get()->info($userId);
        list($buildingList) = BuildingService::get()->info($userId);
        list($orderList, $orderBoardList) = OrderService::get()->info($userId);
        list($plantList) = PlantService::get()->info($userId);
        list($goodsList) = GoodsService::get()->info($userId);
        list($mineList) = MineService::get()->info($userId);
        list($seedList) = SeedService::get()->info($userId);
        $newCultivateLandList = NewCultivateService::get()->info($userId);

        return array(
            $profile,
            $profileInfoList,
            $groundList,
            $buildingList,
            $orderBoardList,
            $orderList,
            $plantList,
            $goodsList,
            $mineList,
            UserService::get()->getTownName($userId),
            $newCultivateLandList,
            $seedList,
        );
    }
}